﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enter.BizLogic;
using Enter.Entity;
using Enter.Config;

namespace Enter.Web.SystemAdmin.GoodClass
{
    public partial class GoodBrandClass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {

            T105_GoodClassBLLSub bll = new T105_GoodClassBLLSub();
            rp_Data.DataSource = bll.GetGoodClassList("0");

            rp_Data.DataTextField = "CName";
            rp_Data.DataValueField = "CNO";
            rp_Data.DataBind();
            rp_Data.Items.Insert(0, new ListItem("", ""));
        }
        private void BindBrank(string cNO)
        {
            T109_GoodBrandBLLSub bll = new T109_GoodBrandBLLSub();
            List<T109_GoodBrandEntity> List = bll.GetGoodBrands(cNO);
          
           List.Sort(CompareDinosByName);
            List<T109_GoodBrandEntity> list1=new List<T109_GoodBrandEntity>();
            if (List != null && List.Count > 0)
            {
                foreach (T109_GoodBrandEntity entity in List)
                {  
                    string bname = entity.BName;
                   
                  //List<T109_GoodBrandEntity> cList1= list1.FindAll(delegate(T109_GoodBrandEntity item) { if (item.BName == bname) return true; else return false; });
                  List<T109_GoodBrandEntity> cList1 = list1.FindAll(model=>model.BName==bname);
                    
                    if (cList1.Count == 0)
                    {
                        string bno = entity.BNO;
                        //用 lamda表达式能省很多代码；
                        List<T109_GoodBrandEntity> cList = list1.FindAll(model => model.BName == bname);
                            //List.FindAll(delegate(T109_GoodBrandEntity item) { if (item.BName == bname) return true; else return false; });
                        T114_GoodBrand_ItemBLLSub bll3 = new T114_GoodBrand_ItemBLLSub();
                        for (int i = 0; i < cList.Count; i++)
                        {
                            T109_GoodBrandEntity e1 = cList[i];

                            T114_GoodBrand_ItemEntity en = new T114_GoodBrand_ItemEntity();
                            en.BNO = bno;
                            en.GoodClassID = e1.GoodClassID;
                            en.status = EnumStatus.Valid;
                            bll3.Save(en);
                            if (i != 0)
                            {
                                bll.Delete(e1.BID);
                            }
                        }

                        list1.Add(entity);
                    }

               
                }
            }
            CheckBoxList1.DataSource=List ;

           // string ss = "";
           // List.RemoveAll(delegate(T109_GoodBrandEntity item) { if (item.BName == ss) return true; else return false; });

            CheckBoxList1.DataBind();
        }
        private static int CompareDinosByName(T109_GoodBrandEntity x, T109_GoodBrandEntity y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're
                    // equal. 
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y
                    // is greater. 
                    return -1;
                }
            }
            else
            {
                // If x is not null...
                //
                if (y == null)
                // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    // ...and y is not null, compare the 
                    // lengths of the two strings.
                    //
                    int retval = x.BName.CompareTo(y.BName);

                    if (retval != 0)
                    {
                        // If the strings are not of equal length,
                        // the longer string is greater.
                        //
                        return retval;
                    }
                    else
                    {
                        // If the strings are of equal length,
                        // sort them with ordinary string comparison.
                        //
                        return 0;
                    }
                }
            }
        }

        private TreeNode BindTree(TreeNode ftn, string parentno)
        {

            T105_GoodClassBLLSub bll = new T105_GoodClassBLLSub();
            List<T105_GoodClassEntity> list = bll.GetGoodClassList(parentno);


           
            if (list != null && list.Count > 0)
            {
                foreach (T105_GoodClassEntity entity in list)
                {
                    TreeNode ztn = new TreeNode();//父结点
                    ztn.Text = entity.CName;
                    ztn.Value = entity.CNO;
                    ftn.ChildNodes.Add(ztn);
                    BindTree(ztn, entity.CNO);
                }
                
            }
            else
            {
                return ftn;
            }
            return ftn;
        }
        private static bool EndsWithSaurus(T105_GoodClassEntity s)
        {
            return true;
        }



        protected void rp_Data_SelectedIndexChanged(object sender, EventArgs e)
        {
            tvGivePop.Nodes.Clear();
            T105_GoodClassBLLSub bll = new T105_GoodClassBLLSub();
            List<T105_GoodClassEntity> list = bll.GetGoodClassList(rp_Data.SelectedValue);

          

             if (list != null && list.Count > 0)
             {
                 foreach (T105_GoodClassEntity entity in list)
                 {
                     TreeNode ztn = new TreeNode();//父结点
                     ztn.Text = entity.CName;
                     ztn.Value = entity.CNO;
                     ztn.ChildNodes.Add(ztn);
                     BindTree(ztn, entity.CNO);
                     tvGivePop.Nodes.Add(ztn);
                 }
             }
             BindBrank(rp_Data.SelectedValue);
        }
    }
}